﻿using full_leaf_framework.Interact;
using full_leaf_framework.Visual;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace hud;

public class Game1 : Game
{
    private GraphicsDeviceManager _graphics;
    private SpriteBatch _spriteBatch;

    public Camera camera;
    public InputManager input;

    public HudController huds;

    public int newCardX = 60;

    private const int SCREEN_WIDTH = 800;
    private const int SCREEN_HEIGHT = 600;

    public Game1() {
        _graphics = new GraphicsDeviceManager(this)
        {
            PreferredBackBufferWidth = SCREEN_WIDTH,
            PreferredBackBufferHeight = SCREEN_HEIGHT
        };
        _graphics.ApplyChanges();
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    protected override void Initialize() {
        base.Initialize();
    }

    protected override void LoadContent() {
        _spriteBatch = new SpriteBatch(GraphicsDevice);
        camera = new Camera(_spriteBatch, new Rectangle(0, 0, 800, 600), new Vector2(400, 300), new Vector2(800, 600));
        huds = new HudController(Content, camera);
        huds.AddHud(@"Data\Hud\card_menu.json");
        input = new InputManager();
        input.InsertTrackingKeys(new Keys[10] {Keys.D1, Keys.D2, Keys.D3, Keys.D4,
        Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0});
        ((CardMenu)huds["card_hud"]).StartMenu(input, camera);
    }

    protected override void Update(GameTime gameTime) {
        input.Update(gameTime);
        TryInsertNewCard();
        huds.Update(gameTime);
        huds.InsertDrawObjects();
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime) {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        _spriteBatch.Begin();
        huds.Draw();
        _spriteBatch.End();
        base.Draw(gameTime);
    }

    public void TryInsertNewCard() {
        Keys[] keys = new Keys[10] {Keys.D1, Keys.D2, Keys.D3, Keys.D4,
        Keys.D5, Keys.D6, Keys.D7, Keys.D8, Keys.D9, Keys.D0};
        int index = 0;
        foreach (Keys key in keys) {
            if (input.GetTrackingKey(key).fired) {
                string message = "Clicked the card : " + (index + 1);
                var newUnit = huds.InsertHudObject("card_hud", "single_card", "single_card" + index, new string[1] {message});
                ((Card)newUnit)?.Translate(new Vector2(newCardX, 200));
                ((Card)newUnit)?.SetFrame(index);
                huds["card_hud"].RunCommandSequence("add_event", newUnit.GetName(), "ButtonActiveEvent", "idle");
                huds["card_hud"].RunCommandSequence("add_event", newUnit.GetName(), "AnimationForward", "focus");
                huds["card_hud"].RunCommandSequence("add_event", newUnit.GetName(), "AnimationBackward", "notFocus");
                huds["card_hud"].RunCommandSequence("add_animation", newUnit.GetName(), "card_select", "current");
                newCardX += 100;
            }
            index++;
        }
    }

}
